package com.rk.kbk.controller;

/**
 * Created by bilibili on 2017/3/13.
 */


import com.rk.kbk.common.FailException;
import com.rk.kbk.common.JsonResp;
import com.rk.kbk.dao.OpenAreaDao;
import com.rk.kbk.dto.PageDto;
import com.rk.kbk.model.OpenArea;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @OpenAreaController
 * @开通地区Controller
 * @version : Ver 1.0
 */
@CrossOrigin
@RestController
@Transactional
@RequestMapping(value="/api/openArea")
public class OpenAreaController {
    private Logger log = Logger.getLogger(this.getClass());

    @Autowired
    private OpenAreaDao openAreaDao;

    /**
     * @添加开通地区
     * @param openArea
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/addArea",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp addArea( @RequestBody OpenArea openArea) {
        log.debug("添加开通地区");
        OpenArea o = openAreaDao.findByAreaCode(openArea.getAreaCode());
        if(o==null){
            openAreaDao.save(openArea);
        }else{
            throw new FailException("该地区已开通");
        }
        return JsonResp.ok();
    }


    /**
     * @所有开通地区
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/allArea",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp allArea() {
        log.debug("所有开通地区");
        return JsonResp.ok(openAreaDao.findAll());
    }

    /**
     * 分页查询(jpa方式)
     * @param pageNum 第几页
     * @param pageSize 每页的条数
     * @return JsonResp
     */
    @RequestMapping(value = "/jpa/page",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp findPageOpenAreaJpa( Integer pageNum,Integer pageSize) {
        log.debug("分页查询(jpa方式)");
        //多条件排序Sort
        //Sort sort = new Sort(Sort.Direction.DESC, "createdate").and(new Sort(Sort.Direction.AES, "id"));
        //排序Sort
        Sort sort = new Sort(Sort.Direction.DESC, "gmtDatetime");
        Pageable pageable = new PageRequest(pageNum-1, pageSize, sort);
        Page page = openAreaDao.findAll(pageable) ;//无条件分页
        return JsonResp.ok(new PageDto(page));
    }

    /**
     * @修改状态
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/updateStatus",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp updateStatus(Long id) {
        log.debug("修改状态");
        OpenArea o = openAreaDao.findOne(id);
        if(o.getStatus()==0){
            o.setStatus(1);
        }else{
            o.setStatus(0);
        }
        openAreaDao.save(o);
        return JsonResp.ok();
    }

    /**
     * @删除开通区域-one(永久)
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/deleteOpenAreaOne",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp deleteOpenAreaOne(Long id) {
        log.debug("删除消息-one(永久)");
        openAreaDao.delete(id);
        return JsonResp.ok();
    }

    /**
     * @根据区域代码获取详情
     * @param areaCode
     * @return 返回值JsonResp
     */
    @RequestMapping(value = "/findByAreaCode",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public JsonResp deleteOpenAreaOne(String areaCode) {
        log.debug("根据区域代码获取详情");
        return JsonResp.ok(openAreaDao.findByAreaCode(areaCode));
    }







}
